package com.lxw.manage.media.service;

import com.lxw.framework.domain.course.response.CourseCode;
import com.lxw.framework.domain.media.MediaFile;
import com.lxw.framework.domain.media.request.QueryMediaFileRequest;
import com.lxw.framework.domain.media.response.MediaCode;
import com.lxw.framework.exception.ExceptionCast;
import com.lxw.framework.model.response.CommonCode;
import com.lxw.framework.model.response.QueryResponseResult;
import com.lxw.framework.model.response.QueryResult;
import com.lxw.framework.model.response.ResponseResult;
import com.lxw.manage.media.dao.MediaFileRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class MediaFileService {
    @Autowired
    private MediaFileRepository mediaFileRepository;
    @Autowired
    private MediaUploadService mediaUploadService;

    public QueryResponseResult findList(Integer page, Integer size, QueryMediaFileRequest queryMediaFileRequest) {
        if (page<=0){
            page = 1;
        }
        if (size<=0){
            size = 10;
        }
        page = page-1;
        //设置分页条件
        Pageable pageable = PageRequest.of(page, size);

        //设置查询条件
        if (queryMediaFileRequest == null){
            queryMediaFileRequest = new QueryMediaFileRequest();
        }
        MediaFile mediaFile = new MediaFile();
        ExampleMatcher matching = ExampleMatcher.matching();
        if (StringUtils.isNotEmpty(queryMediaFileRequest.getTag())){
            mediaFile.setTag(queryMediaFileRequest.getTag());
            matching = matching .withMatcher("tag",ExampleMatcher.GenericPropertyMatchers.contains());
        }
        if (StringUtils.isNotEmpty(queryMediaFileRequest.getProcessStatus())){
            mediaFile.setProcessStatus(queryMediaFileRequest.getProcessStatus());
        }
        if (StringUtils.isNotEmpty(queryMediaFileRequest.getFileOriginalName())){
            mediaFile.setFileOriginalName(queryMediaFileRequest.getFileOriginalName());
            matching = matching .withMatcher("fileOriginalName",ExampleMatcher.GenericPropertyMatchers.contains());
        }

        Example<MediaFile> example = Example.of(mediaFile,matching);

        Page<MediaFile> repositoryAll = mediaFileRepository.findAll(example, pageable);
        long totalElements = repositoryAll.getTotalElements();
        List<MediaFile> content = repositoryAll.getContent();
        QueryResult<MediaFile> queryResult = new QueryResult<>();
        queryResult.setList(content);
        queryResult.setTotal(totalElements);
        QueryResponseResult result = new QueryResponseResult(CommonCode.SUCCESS, queryResult);
        return result;
    }

    public ResponseResult dispose(String id) {
        if (StringUtils.isEmpty(id)){
            ExceptionCast.cast(CommonCode.INIT_PARAM);
        }
        Optional<MediaFile> optional = mediaFileRepository.findById(id);
        if (!optional.isPresent()){
            ExceptionCast.cast(MediaCode.MEDIA_FILE_ISNULL_FAIL);
        }
        MediaFile mediaFile = optional.get();
        String processStatus = mediaFile.getProcessStatus();
        if (!processStatus.equals("303003")){
            return new ResponseResult(CommonCode.FAIL);
        }
        mediaUploadService.sendMessage(mediaFile.getFileId());
        mediaFile.setProcessStatus("303001");
        mediaFileRepository.save(mediaFile);
        return new ResponseResult(CommonCode.SUCCESS);
    }
}
